package com.nenglai.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 公共Util
 *
 * @author kongdehui
 * @date 2021/04/21 19:23
 **/
public class HelperUtil {

    static String UNDERLINE = "_";

    /**
     * 下划线转驼峰
     *
     * @param str
     * @return java.lang.String
     * @author kongdehui
     * @date 2021/04/21 19:24
     */
    public static String lineToHump(String str) {
        if (str == null || "".equals(str.trim())) {
            return "";
        }
        Matcher matcher = Pattern.compile("_(\\w)").matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 驼峰转单词
     *
     * @param param
     * @return java.lang.String
     * @author kongdehui
     * @date 2021/04/21 19:24
     */
    public static String humpToWord(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(' ');
                sb.append(c);
            } else {
                if (i == 0) {
                    c = Character.toUpperCase(c);
                }
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 驼峰转下划线
     *
     * @param param
     * @return java.lang.String
     * @author kongdehui
     * @date 2021/04/21 19:25
     */
    public static String humpToLine(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

}
